/**
 * 
 */
package cn.ad.server.web.servlet.v2.parser;

import cn.ad.server.web.servlet.v2.requests.TrackingRequest;
import cn.ad.server.web.servlet.v2.requests.WinoticeRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Wu Chengjun
 */
public class DefaultWinoticeRequestParser extends TrackingRequestParser {
	
	/**
	 * 默认的竞价成功价格参数名
	 */
	public static final String PARAM_WINOTICE_PRICE = "win_price";
	
	/* (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.TrackingRequestParser#readTrackingParam(javax.servlet.http.HttpServletRequest, cn.falconnect.adserver.adapter.TrackingRequest)
	 */
	@Override
	public void readTrackingParam(HttpServletRequest request, TrackingRequest trackingRequest) throws AdRequestParseException {
		super.readTrackingParam(request, trackingRequest);
		try{
			((WinoticeRequest)trackingRequest).setWin_price(parseWinPrice(request));
		}catch(ParameterParseExcption ex){
			ex.setResult(trackingRequest);
			throw ex;
		}
	}

	/**
	 * 获取winotice参数值
	 * @param request
	 * @return
	 */
	protected String getWinoticePriceParam(HttpServletRequest request){
		return request.getParameter(PARAM_WINOTICE_PRICE);
	}
	
	/**
	 * 从请求中解析并返回竞价成功价格
	 * @param request
	 * @return
	 * @throws AdRequestParseException
	 */
	public double parseWinPrice(HttpServletRequest request) throws AdRequestParseException{
		String winPrice = getWinoticePriceParam(request);
		try{
			return Double.parseDouble(winPrice);
		}catch(NumberFormatException e){
			throw new AdRequestParseException("winPrice[" + winPrice + "] 格式转换异常.", e);
		}
	}

	@Override
	public TrackingRequest createTrackingRequest(HttpServletRequest request) throws AdRequestParseException {
		return new WinoticeRequest();
	}
}
